package com.me.util;

import java.util.Arrays;
import java.util.Random;

import org.springframework.util.Assert;

/**
 * 随机数工具类
 * @author dong
 *
 */
public class RandomUtil {
	/**
	 * 生成指定范围内一组不重复的随机数
	 * @param max		随机数上限
	 * @param amount	要生成随机数的数量
	 * @return
	 */
	public static int[] genRandomNums(int max, int amount){
		Assert.isTrue(amount > 0);
		Assert.isTrue(max > 0);
		Assert.isTrue(max >= amount);
		int[] result = new int[amount];
		boolean[] lock = new boolean[max];
		
		Random rdm = new Random();
		for(int i = 0; i < amount; i++){
			int x = rdm.nextInt(max);
			if(!lock[x]){
				result[i] = x;
				lock[x] = true;
			} else{
				i--;
			}
		}
		
		return result;
	}
	
	public static void main(String[] args) {
		int[] nums = genRandomNums(10, 10);
		System.out.println(Arrays.toString(nums));
	}
}
